package com.hzdl.apple.dao.imp.back;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.hzdl.apple.bean.User;
import com.hzdl.apple.dao.BaseDaoImp;
import com.hzdl.apple.dao.back.BackUserDao;

public class BackUserDaoImp extends BaseDaoImp implements BackUserDao {

	@Override
	public List<User> getAllUser() {
		sql = "select * from user";
		try {
			return qr.query(sql,new BeanListHandler<>(User.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int getUserNum() {
		sql = "select count(*) from user";
		try {
			return qr.query(sql,new BeanHandler<>(Integer.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public boolean addUser(User user) {
		sql = "insert into user values (?,?,?,?,?,?)";
		try {
			qr.update(sql,null,user.getUName(),user.getULoginId(),user.getULoginPsw(),user.getUAddress(),user.getUAddress());
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean updateUser(String uID, User user) {
		sql = "update user set UName = ?,ULoginId = ?,ULoginPsw = ?, UAddress = ?,UPhoneNum = ? where UID = ?";
		try {
			qr.update(sql,user.getUName(),user.getULoginId(),user.getULoginPsw(),user.getUAddress(),user.getUPhoneNum(),uID);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public User getUserByID(String uID) {
		sql = "select * from user where UID = ?";
		try {
			return qr.query(sql, new BeanHandler<>(User.class),uID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean deleteUser(String uID) {
		sql = "delete from user where UID = ?";
		try {
			qr.update(sql,uID);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	
}
